﻿function AzureTableFilterCompareExpression(leftExpr, op, rightExpr) {

    var self = this;

    this.left = leftExpr;

    this.right = rightExpr;

    this.operator = op;

    this.edmType = 'Edm.String';
    
    this.type = 'compare';

    function getValue(rec) {
        return rec.get(self.left);
    }

    function equals(rec) {
        var val = getValue(rec);
        return val == self.right;
    }

    function notEquals(rec) {
        return !equals(rec);
    }

    function greater(rec) {
        var val = getValue(rec);
        return val > self.right;

    }

    function greaterOrEquals(rec) {
        return greater(rec) || equals(rec);
    }

    function less(rec) {
        return !greaterOrEquals(rec);
    }

    function lessOrEquals(rec) {
        return !greater(rec);
    }

    this.match = function (rec) {
        switch (this.operator) {
            case 'eq':
                return equals(rec);
            case 'gt':
                return greater(rec);
            case 'ge':
                return greaterOrEquals(rec);
            case 'lt':
                return less(rec);
            case 'le':
                return lessOrEquals(rec);
            case 'ne':
                return notEquals(rec);
            default:
                throw new Error('unknown operator');
        }
    };
}
